package com.mayisoftware.mayioa.business.statistic.impl.controller;

import com.mayisoftware.mayioa.business.statistic.api.domain.*;
import com.mayisoftware.mayioa.business.statistic.api.service.IStatisticService;
import com.mayisoftware.mayioa.common.impl.utils.StringUtils;
import com.mayisoftware.mayioa.platform.api.domain.SysDept;
import com.mayisoftware.mayioa.platform.api.domain.SysDictData;
import com.mayisoftware.mayioa.platform.api.service.ISysDeptService;
import com.mayisoftware.mayioa.platform.api.service.ISysDictDataService;
import com.mayisoftware.mayioa.platform.impl.web.base.BaseController;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

/**
 * 骨干统计控制器
 */
@Controller
@RequestMapping("/statistic/cu")
public class StatisticCUController extends BaseController{

    /**
     * 前缀
     */
    private String prefix = "statistic/cu";

    /**
     * 统计服务
     */
    @Autowired
    private IStatisticService statisticService;

    /**
     * 最大显示条数
     */
    private final  static Integer MAX_VIEW =5;

    /**
     * 统计页面
     */
    @RequiresPermissions("statistic:cu:view")
    @GetMapping()
    public String list(ModelMap mmap)
    {
        List<StatisDept> list= statisticService.selectCountStatisByDeptAndUserType("CU");
        mmap.put("buTypeList",list);
        return prefix + "/cu";
    }


    /**
     * 根据Bu统计饼图
     */
    @RequiresPermissions("statistic:cu:view")
    @PostMapping("/list/listByPie")
    @ResponseBody
    public EchartsPieVO listByPie()
    {
        EchartsPieVO res = new EchartsPieVO();
        List<StatisDept> list= statisticService.countStatisByDeptType("CU");
        List<String> data = new ArrayList<String>();
        List<SerizeData> serizeData = new ArrayList<SerizeData>();
        res.setData(data);
        res.setSerizeData(serizeData);
        //按数量排序
        Collections.sort(list,new Comparator<StatisDept>(){
            @Override
            public int compare(StatisDept o1, StatisDept o2) {
               return o2.getNum().compareTo(o1.getNum());
            }
        });
        //超过5个显示其他
        SerizeData otherSd = new SerizeData();
        otherSd.setValue(0);
        otherSd.setName("其他");
        for(int i =0;i<list.size();i++){
            StatisDept sd = list.get(i);
            if(i>=MAX_VIEW){
                otherSd.setValue(otherSd.getValue()+sd.getNum());
                continue;
            }
            data.add(sd.getDeptName());
            SerizeData sdata = new SerizeData();
            sdata.setName(sd.getDeptName());
            sdata.setValue(sd.getNum());
            serizeData.add(sdata);
        }
        if(list.size()>MAX_VIEW){
            serizeData.add(otherSd);
            data.add("其他");
        }
        return res;
    }

    /**
     * 根据Bu统计柱状图
     */
    @RequiresPermissions("statistic:cu:view")
    @PostMapping("/list/listByBar")
    @ResponseBody
    public EChartsBarVO listByBgBar(String depts)
    {
        Set<String> deptFilter = new HashSet<String>();
        if(!StringUtils.isEmpty(depts)&&!"null".equals(depts)){
            for(String s :depts.split(",")){
                deptFilter.add(s);
            }
        }
        EChartsBarVO eChartsBarVO = new EChartsBarVO();
        List<StatisDept> list= statisticService.selectCountStatisByDeptAndUserType("CU");
        List<String> data = new ArrayList<String>();
        List<SeriseData> seriesData = new ArrayList<SeriseData>();
        Set<String> xAixsData = new HashSet<String>();
        for(StatisDept sd: list){
            xAixsData.addAll(sd.getProjectRoleCount().keySet());
        }
        eChartsBarVO.setxAixsData(xAixsData);
        eChartsBarVO.setLegnedData(data);
        eChartsBarVO.setSeriesData(seriesData);
        for(int i=0;i<list.size();i++){
            StatisDept sd = list.get(i);
            if(!CollectionUtils.isEmpty(deptFilter)&&!deptFilter.contains(sd.getDeptName())){
                continue;
            }
            data.add(sd.getDeptName());
            SeriseData sData = new SeriseData();
            sData.setName(sd.getDeptName());
            sData.setType("bar");
            sData.setBarWidth("15");
            List<Integer> ndata = new ArrayList<Integer>();
            for(String s :xAixsData){
                ndata.add(sd.getProjectRoleCount().get(s)==null?0:sd.getProjectRoleCount().get(s));
            }
            sData.setData(ndata);
            seriesData.add(sData);
            //判断最多显示5个
            if(seriesData.size()>=MAX_VIEW){
                return eChartsBarVO;
            }
        }
        return eChartsBarVO;
    }


    /**
     * 根据日期统计
     */
    @RequiresPermissions("statistic:bu:view")
    @PostMapping("/list/date")
    @ResponseBody
    public EChartsBarVO listByDate(String depts)
    {
        Set<String> deptFilter = new HashSet<String>();
        if(!StringUtils.isEmpty(depts)&&!"null".equals(depts)){
            for(String s :depts.split(",")){
                deptFilter.add(s);
            }
        }
        EChartsBarVO eChartsBarVO = new EChartsBarVO();
        List<StatisDept> list= statisticService.selectCountStatisByDeptAndDateInfo("CU");
        List<String> data = new ArrayList<String>();
        List<SeriseData> seriesData = new ArrayList<SeriseData>();
        Set<String> xAixsData = new TreeSet<String>();
        for(StatisDept sd: list){
            xAixsData.addAll(sd.getProjectRoleCount().keySet());
        }
        eChartsBarVO.setxAixsData(xAixsData);
        eChartsBarVO.setLegnedData(data);
        eChartsBarVO.setSeriesData(seriesData);
        for(int i=0;i<list.size();i++){
            StatisDept sd = list.get(i);
            if(!CollectionUtils.isEmpty(deptFilter)&&!deptFilter.contains(sd.getDeptName())){
                continue;
            }
            data.add(sd.getDeptName());
            SeriseData sData = new SeriseData();
            sData.setName(sd.getDeptName());
            sData.setType("line");
            List<Integer> ndata = new ArrayList<Integer>();
            for(String s :xAixsData){
                ndata.add(sd.getProjectRoleCount().get(s)==null?0:sd.getProjectRoleCount().get(s));
            }
            sData.setData(ndata);
            seriesData.add(sData);
            //判断最多显示5个
            if(seriesData.size()>=MAX_VIEW){
                return eChartsBarVO;
            }
        }
        return eChartsBarVO;
    }

}
